package com.clean.notification

import android.annotation.SuppressLint
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import kotlin.jvm.internal.Intrinsics

@SuppressLint("StaticFieldLeak")
object ShowUtils {


    fun req(context: Context, next: () -> Unit) {
        XXPermissions.with(context).permission(Permission.POST_NOTIFICATIONS)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    next.invoke()
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    super.onDenied(permissions, doNotAskAgain)
                    next.invoke()
                }
            })
    }


    fun isNotificationEnabled(context: Context): Boolean {
        var isEnable = true
        if (Build.VERSION.SDK_INT >= 26) {
            isEnable = if (isEnableV26(context)) {
                true
            } else {
                false
            }
        }
        return isEnable
    }

    private fun isEnableV26(context: Context): Boolean {
        try {
            var var10000 = context.getSystemService(Context.NOTIFICATION_SERVICE)
            if (var10000 == null) {
                throw NullPointerException("null cannot be cast to non-null type android.app.NotificationManager")
            } else {
                val notificationManager = var10000 as NotificationManager
                val sServiceField = notificationManager.javaClass.getDeclaredMethod("getService")
                Intrinsics.checkNotNullExpressionValue(sServiceField, "sServiceField")
                sServiceField.isAccessible = true
                val sService = sServiceField.invoke(notificationManager)
                val appInfo = context.applicationInfo
                val var10 = context.applicationContext
                Intrinsics.checkNotNullExpressionValue(var10, "context.applicationContext")
                val pkg = var10.packageName
                val uid = appInfo.uid
                val method = sService.javaClass.getDeclaredMethod(
                    "areNotificationsEnabledForPackage", String::class.java, Integer.TYPE
                )
                Intrinsics.checkNotNullExpressionValue(method, "method")
                method.isAccessible = true
                var10000 = method.invoke(sService, pkg, uid)
                if (var10000 == null) {
                    throw NullPointerException("null cannot be cast to non-null type kotlin.Boolean")
                } else {
                    return var10000 as Boolean
                }
            }
        } catch (var25: java.lang.Exception) {
            var25.printStackTrace()
            return false
        }
    }
}